package cn.itsource.order.service.impl;

import cn.itsource.base.service.impl.BaseServiceImpl;
import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.PageList;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.domain.dto.OrderPetConfirmDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public AjaxResult pageList(OrderPetAcquisitionQuery query,Long logininfoId) {
        if(query.getCurrentPage() < 1){
            throw new RuntimeException("");
        }
        Shop shop = shopMapper.selectByLoginId(logininfoId);
        if(shop == null){
           return AjaxResult.me().setSuccess(false).setMessage("你没有店铺");
        }
        query.setShopId(shop.getId());
        //分页查询
        List<OrderPetAcquisition> orderPetAcquisitions = orderPetAcquisitionMapper.query(query);
        //创建分页接收对象
        PageList<OrderPetAcquisition> pageList = new PageList();
        //设置分页数据
        pageList.setData(orderPetAcquisitions);
        //设置分页总条数
        pageList.setCount(orderPetAcquisitionMapper.queryCount(query));

        return AjaxResult.me().setResultObj(pageList);
    }

    @Override
    @Transactional
    public AjaxResult confirm(OrderPetConfirmDto dto) {
        Long id = dto.getId();
        //订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectById(id);
        //查询寻主消息
        Long searchMasterMsgId = orderPetAcquisition.getSearchMasterMsgId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(searchMasterMsgId);
        //1添加一个宠物 到店铺里面去
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(dto.getMoney());
        pet.setSaleprice(dto.getMoney());
        pet.setTypeId(searchMasterMsg.getPetType());
        pet.setResources(searchMasterMsg.getResources());
        pet.setState(0);
        Date date = new Date();
        pet.setOffsaletime(date);
        pet.setCreatetime(date);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setUserId(searchMasterMsg.getUserId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petMapper.save(pet);//保存宠物主表
        PetDetail petDetail = new PetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.save(petDetail);
        //2修改状态  订单
        orderPetAcquisition.setPrice(dto.getMoney());
        orderPetAcquisition.setPaytype(dto.getPayType());
        orderPetAcquisition.setState(1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //3修改寻主消息的状态
        searchMasterMsg.setState(3);
        searchMasterMsgMapper.update(searchMasterMsg);
        return AjaxResult.me();
    }
}
